package com.eternal.framework.component;

import com.afollestad.materialdialogs.MaterialDialog;
import com.eternal.framework.utils.MaterialDialogUtils;
import com.trello.rxlifecycle2.components.support.RxFragment;

import io.reactivex.disposables.Disposable;

/**
 * @author li
 */
public class BaseRxFragment extends RxFragment {
    private MaterialDialog dialog;
    private DismissListener listener;


    public void showDialog(String title, final Disposable disposable) {
        if (dialog != null) {
            listener.setDisposable(disposable);
            dialog = dialog.getBuilder()
                    .title(title)
                    .build();
            dialog.show();
        } else {
            listener =new DismissListener(disposable);
            MaterialDialog.Builder builder = MaterialDialogUtils.showIndeterminateProgressDialog(getActivity(), title, true)
                    .dismissListener(listener);
            dialog = builder.show();
        }
    }

    public void dialogDisposable(Disposable disposable) {
        listener.setDisposable(disposable);
    }

    public void dismissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }
}
